<template>
  <!-- 法规平台 -->
  <div class="app-container">
    <!-- <h1>法规平台</h1> -->
    <div class="boxlaw">
      <div slot="header" class="clearfix">
        <ComSelect @EventChange="ChangeFn" :options="options"></ComSelect>
      </div>

      <div class="comform">
        <ComForm
          ref="comForm"
          @EventReset="resetFn"
          @EventOnsubmit="queryFn"
          :list="comForm"
        >
        </ComForm>
      </div>

      <ComTable
        @handleCurrentChange="CurrentPageChangeFn"
        @handleSizeChange="currentPageSizeChangeFn"
        :key="random"
        :list="tableList"
        :modes="tableModes"
        :config="{ border: true, loading }"
        :pagination="{ total: paging.total, pageNum: paging.pageNum }"
      >
        <el-table-column
          #default="{ row }"
          align="left"
          width="100"
          label="操作"
        >
          <template v-if="row.status !== 5">
            <!-- "
              $router.push({
                path: '/contentPlatform/laws/laws-details',
                query: row,
              })
            " -->
            <!-- v-hasPermi="['base:docModule:list']" -->
            <el-button @click="clickLook(row)" plain size="mini" type="primary"
              >查看</el-button
            >
          </template>
          <template v-else-if="row.status === 5">
            <el-button
              @click="
                $router.push({
                  query: row,
                })
              "
              plain
              size="mini"
              type="primary"
              >编辑</el-button
            >
            <el-button plain size="mini" type="danger">上传附件</el-button>
          </template>
        </el-table-column>
      </ComTable>
    </div>
  </div>
</template>

<script>
import ComForm from "../../../components/comForm.vue";
import ComSelect from "../../../components/comSelect.vue";
import ComTable from "../../../components/comTable.vue";
import comForm from "../../../components/comForm.vue";
// import { getdocGetTypeApi } from "@/wss/api/ContentSupervision.js";

import { getLawListApi } from "@/wss/api/doc/law.js";
export default {
  name: "laws",
  dicts: ["sys_normal_disable"],
  components: { ComSelect, ComForm, comForm, ComTable },

  data() {
    return {
      initSelectValue: "",
      // 加载
      loading: false,
      paging: { pageNum: 1, pageSize: 10, status: "", total: 10 },
      // 切换筛选状态，不更新bug修复
      random: "",
      // 当前筛选的状态
      currentSelect: "",
      options: {
        value: "",
        title: "快速筛选",
        list: [
          {
            value: "",
            label: "全部",
          },
          {
            value: "0",
            label: "待编辑",
          },
          {
            value: "1",
            label: "审核中",
          },
          {
            value: "2",
            label: "审核失败",
          },

          {
            value: "4",
            label: "上架",
          },
          {
            value: "5",
            label: "下架",
          },
          {
            value: "9",
            label: "编辑中",
          },
        ],
      },
      // 动态生成下拉列表的筛选条件
      dynamicFilter: {},
      // 表格的数据
      tableList: [],
      tableModes: [
        {
          type: "index",
          label: "编号",
          mode: "index",
        },
        {
          width: 200,
          label: "法条",
          mode: "title",
          showOverflowTooltip: true,
        },
        {
          label: "效力级别",
          mode: "type",
        },
        {
          label: "来源",
          mode: "from",
          showOverflowTooltip: true,
        },
        {
          label: "状态",
          mode: "effectiveStatus",

          width: 100,
          align: "center",
          isShowTag: true,
          effectiveStatus: true,
          options: [
            {
              l: "有效",
              v: 0,
            },
            {
              l: "部分有效",
              v: 1,
              type: "warning",
            },
            {
              l: "失效",
              v: 2,
              type: "danger",
            },
            {
              l: "尚未生效",
              v: 3,
            },
          ],
        },
        {
          label: "发行日期",
          mode: "publishTime",
        },
        {
          label: "生效日期",
          mode: "effectiveTime",
        },
        // {
        //   label: "审核",
        //   mode: "status",
        //   width: 100,
        //   align: "center",
        //   isShowTag: true,
        //   options: [
        //     {
        //       l: "待编辑",
        //       v: 0,
        //     },
        //     {
        //       l: "审核失败",
        //       v: 2,
        //       type: "danger",
        //     },
        //     {
        //       l: "审核中",
        //       v: 1,
        //       type: "warning",
        //     },
        //     {
        //       l: "成功",
        //       v: 4,
        //     },
        //   ],
        // },
        {
          label: "审核",
          mode: "status",
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "待编辑",
              v: 0,
            },
            {
              l: "审核失败",
              v: 2,
              type: "danger",
            },
            {
              l: "审核中",
              v: 1,
              type: "warning",
            },
            {
              l: "成功",
              v: 3,
            },
            {
              l: "上架",
              v: 4,
            },
            {
              l: "下架",
              v: 5,
            },
          ],
        },
      ],
      comForm: {
        modes: [
          {
            placeholder: "请输入法规",
            formType: "input",
            mode: "name",
            label: "法条",
            value: "",
          },
          {
            placeholder: "请输入法规",
            formType: "select",
            mode: "type",
            label: "分类",
            value: "",
            select: [],
          },
          {
            formType: "select",
            mode: "from",
            label: "发布来源",
            value: "",
            select: [],
          },
          {
            placeholder: "",
            formType: "select",
            mode: "scope",
            label: "使用区域",
            value: "",
            select: [],
          },
          {
            placeholder: "请选择日期",
            formType: "date",
            mode: "publishTime",
            label: "发布日期",
            value: null,
            pickerType: "datetime",
          },
          {
            placeholder: "请选择日期",
            formType: "date",
            mode: "effectiveTime",
            label: "生效日期",
            value: null,
            pickerType: "datetime",
          },
          {
            placeholder: "请选择日期",
            formType: "select",

            mode: "effectiveStatus",
            label: "状态",
            value: "",
            select: [],
          },
        ],
      },
    };
  },

  mounted() {
    this.getLawListApiMet(this.paging);
  },
  methods: {
    // 重设函数
    resetFn() {
      this.initSelectValue = "";
      this.romdom = Math.random();
      this.options.value = "";
      this.paging.status = "";
      console.log(this.paging, "重设");
    },
    // 当前页发生改变时候
    CurrentPageChangeFn(pageNUm) {
      this.paging.pageNum = pageNUm;
      this.getLawListApiMet(this.paging);
    },
    // 页容量发生改变时候
    currentPageSizeChangeFn(pageSize) {
      this.paging.pageSize = pageSize;
      this.getLawListApiMet(this.paging);
    },
    // 筛选数据
    queryFn(arr) {
      arr.forEach((item) => {
        for (const key in item) {
          this.paging[key] = item[key];
        }
      });
      this.currentSelect = this.initSelectValue;

      this.paging.pageNum = 1;
      this.getLawListApiMet(this.paging);
    },
    // 获取列表数据
    async getLawListApiMet(pagination = null) {
      this.loading = true;
      let result = await getLawListApi(pagination).then((res) => res);

      console.log(result.data.rows, "法规返回的数据");
      if (result.code == 200) {
        this.tableList = result.data.rows;
        this.paging.total = result.data.total;
        // 把后端放回的字段变成下拉筛选字段
        this.tableList.forEach((item) => {
          //是否有这个属性
          this.dynamicFilter.from ? "" : (this.dynamicFilter.from = []);
          this.dynamicFilter.scope ? "" : (this.dynamicFilter.scope = []);
          this.dynamicFilter.type ? "" : (this.dynamicFilter.type = []);
          this.dynamicFilter.effectiveStatus
            ? ""
            : (this.dynamicFilter.effectiveStatus = []);

          if (!this.dynamicFilter.from.includes(item.from)) {
            this.dynamicFilter.from.push(item.from);
          }

          if (!this.dynamicFilter.type.includes(item.type)) {
            this.dynamicFilter.type.push(item.type);
          }

          if (!this.dynamicFilter.scope.includes(item.scope)) {
            this.dynamicFilter.scope.push(item.scope);
          }

          if (
            !this.dynamicFilter.effectiveStatus.includes(item.effectiveStatus)
          ) {
            this.dynamicFilter.effectiveStatus.push(item.effectiveStatus);
          }
        });

        this.comForm.modes.forEach((item) => {
          switch (item.mode) {
            case "from":
              item.select = this.arrTransformObj(this.dynamicFilter.from);
              break;
            case "type":
              item.select = this.arrTransformObj(this.dynamicFilter.type);
              break;
            case "scope":
              item.select = this.arrTransformObj(this.dynamicFilter.scope);
              break;

            case "effectiveStatus":
              item.select = this.arrTransformObj(
                this.dynamicFilter.effectiveStatus,
                ["有效", "部分有效", "失效", "尚未生效"]
              );

              break;
            default:
              break;
          }
        });

        this.loading = false;
      }
    },
    async ChangeFn(value) {
      this.tableModes = [
        {
          type: "index",
          label: "编号",
          mode: "index",
        },
        {
          label: "法条",
          mode: "title",
          showOverflowTooltip: true,
        },
        {
          label: "效力级别",
          mode: "type",
        },
        {
          label: "来源",
          mode: "from",
        },
        {
          label: "状态",
          mode: "effectiveStatus",
          effectiveStatus: true,
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "有效",
              v: 0,
            },
            {
              l: "部分有效",
              v: 1,
              type: "warning",
            },
            {
              l: "失效",
              v: 2,
              type: "danger",
            },
            {
              l: "尚未生效",
              v: 3,
            },
          ],
        },
        {
          label: "发行日期",
          mode: "publishTime",
        },
        {
          label: "生效日期",
          mode: "effectiveTime",
        },
        {
          label: "审核",
          mode: "status",
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "待编辑",
              v: 0,
            },
            {
              l: "审核失败",
              v: 2,
              type: "danger",
            },
            {
              l: "审核中",
              v: 1,
              type: "warning",
            },
            {
              l: "上架",
              v: 4,
            },
            {
              l: "下架",
              v: 5,
            },
            {
              l: "编辑中",
              v: 9,
            },
          ],
        },
        {
          label: "适用区域",
          mode: "scope",
          width: "100",
        },
      ];
      console.log(value);
      // 成功就是展示中
      if (value == 4) {
        this.tableModes.push({
          label: "生效时间",
          mode: "effectiveTime",
        });
        this.tableModes.push({
          label: "失效时间", //后端说更新时间===失效时间
          mode: "updateTime",
        });
      } else if (value == 1) {
        this.tableModes.push({
          label: "失效时间",
          mode: "updateTime",
        });

        this.tableModes.push({
          label: "提交审核时间",
          mode: "updateTime",
        });
      } else if (value == 2 || value == 3) {
        this.tableModes.push({
          label: "审核人",
          mode: "verifyName",
        });
      }
      this.$refs.comForm.resetFn();
      // 更新是状态（如代编辑）
      this.currentSelect = value;
      this.paging = {
        pageNum: 1,
        pageSize: 10,
        status: "",
        total: 10,
      };
      this.options.value = value;
      this.paging.status = value;
      this.getLawListApiMet(this.paging);
    },

    // 查看
    async clickLook(row) {
      // // 获取esid
      // let { rows } = await getdocGetTypeApi({ type: "法规" });
      // // console.log(rows, 11111111111);
      // let currentItem = rows.find((item) => {
      //   return row.idStr === item.esId;
      // });
      // console.log(currentItem, "2321312");
      // console.log("row----", row);
      // return;
      // 是否是失败
      if (row.status == 2) {
        this.$router.push({
          path: "/contentPlatform/laws/laws-failReason",
          query: { id: row.idStr },
        });
      } else {
        this.$router.push({
          path: "/contentPlatform/laws/laws-details",
          query: { id: row.idStr },
        });
      }
    },
  },
};
</script>

<style lang ="scss" scoped>
.comform {
  margin-top: 20px;
}
</style>